<?php

namespace App\Modules\Apicrzs\Services;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Member\BankModel;
use App\Modules\Apicrzs\Entities\UserBanksModel;
use App\Modules\Apicrzs\Entities\UserScoreModel;
use App\Traits\CryptAES;
use Illuminate\Support\Facades\DB;

class UserBankService extends BaseServices
{
    public function __construct(UserBanksModel $model)
    {
        $this->model = $model;

        $token =  \request()->header('token');
        $user_info = CryptAES::decryptToken($token,'user_grzq');

        $this->user_id = $user_info['data']['user_id']??"";
        $this->model = $model;
    }

    public function getLists($params){
        return $this->model->with(['bank_info'])->where(['user_id'=>$this->user_id])->get()->toArray();
    }
    public function create($params){
        $params['user_id'] = $this->user_id;
        $bank_info = BankModel::getInstance()->getBankById($params['bank_id']);
        DB::beginTransaction();
        try{
            $params['id_card_pic'] = json_encode($params['id_card_pic']);
            $params['bank_name'] = $bank_info['name']??"";
            parent::create($params); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
    public function update(array $params)
    {
        $params['user_id'] = $this->user_id;
        DB::beginTransaction();
        try{
            parent::update($params); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
    public function getUserBankLists($params){

    }
}
